package com.gf.event.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gf.base.type.EventType;
import com.gf.base.type.OperationType;
import com.gf.common.bean.Page;
import com.gf.common.util.StrArrayHelper;
import com.gf.event.dao.EventDao;
import com.gf.event.model.Event;
import com.gf.event.service.EventService;
import com.gf.user.dao.IdolFanDao;
import com.gf.user.model.User;
import com.gf.user.service.UserService;

@Service("eventService")
@Transactional
public class EventServiceImpl implements EventService {

	@Autowired
	private EventDao eventDao;

	@Autowired
	private IdolFanDao idolFanDao;
	
	@Autowired
	private UserService userService;
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public long addEvent(long userId, String userName, String operation,
			String title, long targetId, EventType eventType) {
		User u=userService.getUserById(userId);
		return eventDao.insertEvent(userId, userName, operation, title,u.getPortrait(),
				targetId, eventType);
	}

	@Override
	public Page getEventsByUserId(long userId, int currentPage, int pageSize) {
		Page page = new Page();
		page.setCurpage(currentPage);
		page.setPageSize(pageSize);
		List<Event> result = eventDao.queryEventsByUserId(userId, page);
		page.setResults(result);

		return page;
	}

	@Override
	public Page getEventsOfIdols(long userId, int currentPage, int pageSize) {
		Page page = new Page();
		page.setCurpage(currentPage);
		page.setPageSize(pageSize);

		String idolstr = idolFanDao.queryIdols(userId);
		List<Long> list = StrArrayHelper.strToArray2(idolstr, "-");

		List<Event> result = eventDao.queryEventsOfIdols(userId, list, page);
		page.setResults(result);

		return page;
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public long publishProjectEvent(long userId, String userName, String title,
			long targetId) {
		User u=userService.getUserById(userId);
		return eventDao.insertEvent(userId, userName, OperationType.FA_BU,
				title,u.getPortrait(), targetId, EventType.PROJECT);
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public long joinProjectEvent(long userId, String userName, String title,
			long targetId) {
		User u=userService.getUserById(userId);
		return eventDao.insertEvent(userId, userName, OperationType.CAN_JIA,
				title, u.getPortrait(),targetId, EventType.PROJECT);
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public long attentionEvent(long userId, String userName, String idolname,
			long targetId) {
		User u=userService.getUserById(userId);
		return eventDao.insertEvent(userId, userName, OperationType.GUAN_ZHU,
				idolname,u.getPortrait(), targetId, EventType.ATTENTION);
	}

	@Override
	public Page getAllEvents(int curpage, int pageSize) {
		Page page = new Page();
		page.setCurpage(curpage);
		page.setPageSize(pageSize);

		List<Event> result = eventDao.queryAllEvent(page);
		page.setResults(result);

		return page;
	}

	@Override
	public int changePortrait(long userId,String newPortrait) {

		return eventDao.updatePortrait(userId, newPortrait);
	}

	@Override
	public Page getqueryRecentEvent(int curpage, int pageSize) {
		Page page=new Page();
		page.setCurpage(curpage);
		page.setPageSize(pageSize);
		//System.out.println(eventDao.queryRecentEvent(page).size()+"9999999999");
		page.setResults(eventDao.queryRecentEvent(page));
		return page;
	}

	@Override
	public boolean deleteProjectEvent(long projectId) {
		return eventDao.deleteEvent(projectId, EventType.PROJECT);
	}

}
